package ventanas;

import isi_ib.MyContract;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JCheckBox;

public class Vcontrato extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField tm_symbol;
	private JTextField tm_expiry;
	private JTextField tm_exchange;
	private JTextField tm_currency;
	private JComboBox cbsecType;
	private JCheckBox chckbxIncludeexpired;
	
	private MyContract ccontrato_;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
	/*	try {
			Vcontrato dialog = new Vcontrato();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}*/
	}

	/**
	 * Create the dialog.
	 */
	public Vcontrato(MyContract ccontrato_) {
	
		
		setBounds(100, 100, 337, 262);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		JLabel label = new JLabel("Symbol");
		label.setBounds(10, 17, 113, 14);
		contentPanel.add(label);
		this.ccontrato_=ccontrato_;
		
		
		tm_symbol = new JTextField();
		tm_symbol.setText("ES");
		tm_symbol.setColumns(10);
		tm_symbol.setBounds(133, 11, 86, 20);
		contentPanel.add(tm_symbol);
		
		JLabel label_1 = new JLabel("secType ");
		label_1.setBounds(10, 45, 113, 14);
		contentPanel.add(label_1);
		
		tm_exchange = new JTextField();
		tm_exchange.setColumns(10);
		tm_exchange.setBounds(133, 106, 86, 20);
		contentPanel.add(tm_exchange);
		
		cbsecType = new JComboBox();
		cbsecType.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(((JComboBox)e.getSource()).getSelectedIndex()>=0){
					if(((JComboBox)e.getSource()).getSelectedItem()=="FUT")
						tm_exchange.setText("GLOBEX");
					else tm_exchange.setText("SMART");	
				}
				
			}
		});
		cbsecType.setModel(new DefaultComboBoxModel(new String[] {"STK", "OPT", "FUT", "IND", "FOP", "CASH", "BAG"}));
		cbsecType.setSelectedIndex(2);
		cbsecType.setToolTipText("");
		cbsecType.setBounds(133, 42, 86, 20);
		contentPanel.add(cbsecType);
		
		JLabel label_2 = new JLabel("expiry(YYYYMM)");
		label_2.setBounds(10, 79, 113, 14);
		contentPanel.add(label_2);
		
		tm_expiry = new JTextField();
		tm_expiry.setText("201209");
		tm_expiry.setColumns(10);
		tm_expiry.setBounds(133, 73, 86, 20);
		contentPanel.add(tm_expiry);
		
		JLabel label_3 = new JLabel("exchange");
		label_3.setBounds(10, 112, 113, 14);
		contentPanel.add(label_3);
		
		
		
		JComboBox comboBox_1 = new JComboBox();
		comboBox_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				tm_exchange.setText(((JComboBox)e.getSource()).getSelectedItem().toString());
			}
		});
		comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"SMART", "GLOBEX"}));
		comboBox_1.setSelectedIndex(0);
		comboBox_1.setBounds(229, 106, 86, 20);
		contentPanel.add(comboBox_1);
		
		tm_currency = new JTextField();
		tm_currency.setText("USD");
		tm_currency.setColumns(10);
		tm_currency.setBounds(133, 134, 86, 20);
		contentPanel.add(tm_currency);
		
		JLabel label_4 = new JLabel("currency ");
		label_4.setBounds(10, 140, 113, 14);
		contentPanel.add(label_4);
		
		chckbxIncludeexpired = new JCheckBox("IncludeExpired");
		chckbxIncludeexpired.setBounds(6, 161, 97, 23);
		contentPanel.add(chckbxIncludeexpired);
		
		
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						okContrato();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						cancelContrato();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
	public void okContrato(){
		AsignaValores();
		ccontrato_.Guarda();
	}
	public void cancelContrato(){
		this.setVisible(false);
	}
	void AsignaValores(){
		ccontrato_.setM_symbol(tm_symbol.getText());
		ccontrato_.setM_currency(tm_currency.getText());
		ccontrato_.setM_exchange(tm_exchange.getText());
		ccontrato_.setM_expiry(tm_expiry.getText());
		ccontrato_.setM_secType(cbsecType.getSelectedItem().toString());
		ccontrato_.setM_includeExpired(chckbxIncludeexpired.isSelected());
		
	}
	void RecogeValores(){
		tm_symbol.setText(ccontrato_.contrato.m_symbol);
		tm_currency.setText(ccontrato_.contrato.m_currency);		
		tm_exchange.setText(ccontrato_.contrato.m_exchange);
		tm_expiry.setText(ccontrato_.contrato.m_expiry);
		cbsecType.setSelectedItem(ccontrato_.contrato.m_secIdType);
		chckbxIncludeexpired.setSelected(ccontrato_.contrato.m_includeExpired);
	}
}
